﻿/*-------------------------------------------------------------------------
 * CLR版本:     4.0.30319.42000
 * 创建人:      song
 * 创建时间：   2024/4/9 17:47:27
 * 功 能：      通用编译器界面
 *-------------------------------------------------------------------------*/

#if UNITY_EDITOR
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace Function.SerializedPropertyAttribute
{
    [CanEditMultipleObjects]
    public class ObjectDrawEditor<T> : UnityEditor.Editor where T : UnityEngine.Object
    {
        private FoldoutAttributeHelper _foldout;
        private FoldoutMustAttributeHelper _foldoutMust;
        private ButtonMethodHandler _buttonHandle;
        private T _target; //目标脚本

        private void OnEnable()
        {
            _target = target as T;
            if (target == null) return;

            _foldout = new FoldoutAttributeHelper(target, serializedObject);
            _foldoutMust = new FoldoutMustAttributeHelper(target, serializedObject);
            _buttonHandle = new ButtonMethodHandler(target);
        }

        private void OnDisable()
        {
            _foldout?.OnDisable();
            _foldoutMust?.OnDisable();
        }

        public override void OnInspectorGUI()
        {
            if (_foldout != null)
            {
                _foldout.Update();
            }

            if (_foldoutMust != null)
            {
                _foldoutMust.Update();
            }

            if (_foldout != null && _foldout.OverrideInspector)
            {
                _foldout.OnInspectorGUI();
            }
            else if (_foldoutMust != null && _foldoutMust.OverrideInspector)
            {
                _foldoutMust.OnInspectorGUI();
            }
            else
            {
                base.OnInspectorGUI();
            }

            _buttonHandle.OnInspectorGUI();
            serializedObject.ApplyModifiedProperties();
        }
    }
}
#endif